package org.edu.modules.sbzc.fund.controller;

import cn.hutool.core.convert.Convert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.edu.common.dto.ExcelPo;
import org.edu.common.utils.PoiUtils;
import org.edu.modules.sbzc.fund.dto.VeSbzcZcMaintainDto;
import org.edu.modules.sbzc.fund.entity.VeSbzcZcMaintain;
import org.edu.modules.sbzc.fund.enums.AssetStatusEnum;
import org.edu.modules.sbzc.fund.service.VeSbzcZcAssetService;
import org.edu.modules.sbzc.fund.service.VeSbzcZcMaintainService;
import org.edu.common.api.vo.Result;
import org.edu.common.base.PageDto;
import org.edu.common.dto.StatusDto;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Api(tags = {"设备资产维修表"})
@RestController
@RequestMapping("/sbzc/veSbzcZcMaintain")
@Slf4j
public class VeSbzcZcMaintainController{

    @Autowired
    private VeSbzcZcMaintainService veSbzcZcMaintainService;

    /**
     * VeSbzcZcAssetService
     */
    @Autowired
    private VeSbzcZcAssetService veSbzcZcAssetService;

    @ApiOperation("分页查询设备资产维修表")
    @PostMapping("/page")
    public Result<?> page(@RequestBody PageDto<VeSbzcZcMaintain> page) {
        QueryWrapper<VeSbzcZcMaintain> queryWrapper =  page.buildQueryWrapper(VeSbzcZcMaintain.class);
        return Result.OK(veSbzcZcMaintainService.page(page, queryWrapper));
    }

    @ApiOperation("导出数据）")
    @PostMapping("/export")
    public Result<?> export(@RequestBody PageDto<VeSbzcZcMaintain> page, HttpServletResponse resp) {
        QueryWrapper<VeSbzcZcMaintain> queryWrapper =  page.buildQueryWrapper(VeSbzcZcMaintain.class);
        List<ExcelPo> list = new ArrayList<>();
        list.add(new ExcelPo("资产ID","assetId"));
        list.add(new ExcelPo("所属部门","deptOwn"));
        list.add(new ExcelPo("维修单位","deptMaintain"));
        list.add(new ExcelPo("维修人","maintainUserId"));
        list.add(new ExcelPo("批准人,默认为当前操作管理员","checkUserId"));
        list.add(new ExcelPo("","maintainDate"));
        list.add(new ExcelPo("情况说明","descrption"));
        list.add(new ExcelPo("创建时间","createTime"));
        list.add(new ExcelPo("备注","remark"));
        list.add(new ExcelPo("","assetStatus"));
        list.add(new ExcelPo("维修状态（1.维修完成，2 维修中）","status"));
        list.add(new ExcelPo("维修完成时间","maintainFinishDate"));
                PoiUtils.downloadFile(resp,veSbzcZcMaintainService.page(page, queryWrapper).getRecords(),list);
        return Result.OK();
    }

    @AutoLog(value = "设备资产维修表-分页列表查询")
    @ApiOperation(value="设备资产维修表-分页列表查询", notes="设备资产维修表-分页列表查询")
    @PostMapping(value = "/list")
    public Result<?> queryPageList(VeSbzcZcMaintain entity,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<VeSbzcZcMaintain> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        PageDto<VeSbzcZcMaintain> page = new PageDto<VeSbzcZcMaintain>(pageNo, pageSize);
        IPage<VeSbzcZcMaintain> pageList = veSbzcZcMaintainService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @AutoLog(value = "通过id查询")
    @ApiOperation(value="通过id查询", notes="通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        VeSbzcZcMaintain dto = veSbzcZcMaintainService.findDtoById(id);
        if(dto==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(dto);
    }

    @AutoLog(value = "添加")
    @ApiOperation(value="添加", notes="添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeSbzcZcMaintainDto dto) {
        veSbzcZcMaintainService.insertOrUpdate(dto , true);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "编辑")
    @ApiOperation(value="编辑", notes="编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeSbzcZcMaintainDto dto) {
        veSbzcZcMaintainService.insertOrUpdate(dto , false);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "批量删除")
    @ApiOperation(value="批量删除", notes="批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        veSbzcZcMaintainService.delete(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }


    @AutoLog(value = "更新状态")
    @ApiOperation(value="更新状态")
    @PostMapping(value = "/updateStatus")
    public Result<?> updateStatus(@RequestBody StatusDto statusDto) {
        UpdateWrapper<VeSbzcZcMaintain> updateWrapper = new UpdateWrapper<>();
        if(statusDto.getIds()!=null && statusDto.getIds().size()>0){
            updateWrapper.set("status",statusDto.getStatus());
            updateWrapper.set("maintain_finish_date",new Date());
            updateWrapper.in("id",statusDto.getIds());
            veSbzcZcMaintainService.update(updateWrapper);
            List<String> ids = statusDto.getIds();
            for(String id :ids){
                int assetId = veSbzcZcMaintainService.findDtoById(id).getAssetId();
                veSbzcZcAssetService.updateAssetStatus(Convert.toStr(assetId), AssetStatusEnum.UNALLOCATED.getValue());
            }
            return Result.OK("批量更新成功!");
        }else{
            return Result.error("未选择!");
        }
    }




}
